<?php
/*
 * This is a smarty function
 * Use to build list of category to Menuitem format
 * in smarty
 */
 
function build_left_menu_item($params) 
{ 
	if(empty($params['cats']))
	{ 
	   return ""; 
	} 
	else 
	{ 
	   $cats = $params['cats'];
	   $str = '<ol id="root" style="display:inherit">';
	   $str .= build_left_li($cats, 0);
	   $str .= '</ol>'; 
	   
	   return $str;
	} 
}

function build_left_li($array, $level)
{
	$str = "";
	
	$tab = "";
	if($level > 0)
	{
		$tab = str_repeat("", $level*2);
	}
	
	foreach($array as $cat)
	{
	
		$str .= sprintf('<li id="%s" >' .
				'<span class="_class_" onclick="_event_">' .
				'<a {_link_} title="%s">' .'<span class="icon"></span>'.
				 $tab. '%s</a></span>',
				"left_item_" . $cat->cat_ID,
				$cat->cat_description,
				$cat->cat_name);
		
		if($cat->sub != null)
		{
			$str = str_replace("_event_", "toggleSub('sub_$cat->cat_ID')" ,$str);
			
			$str = str_replace("_class_", "itemwrap",$str);
			
			$str = str_replace("{_image_}",'<img src="images/icon_left.gif"/>',$str);
			
			$str = str_replace("{_imageactive_}",'<img src="images/icon_active.gif"/>',$str);
			
			$str = str_replace("{_link_}", "", $str);
			
			$str .= "<ul id=\"sub_$cat->cat_ID\" style=\"display:none\">";
			$str .= build_left_li($cat->sub, $level+1);
			$str .= "</ul>";
		}
		else
		{
			$str = str_replace("_event_", "" , $str);
			$str = str_replace("_class_", "menu_item" ,$str);
			$str = str_replace("{_image_}",'<img src="images/icon_left.gif"/>', $str);
			$str = str_replace("{_imageactive_}",'<img src="images/icon_active.gif"/>', $str);
			$str = str_replace("{_link_}", " href=\"" . build_left_link($cat) . "\" ", $str);
		}
		$str .= "</li>";
		
	}
	
	return $str;
}

function build_left_link($cat)
{
	if( $_GET['cat'] == null)
	{
		return "?page=products&amp;cat=" . $cat->cat_ID;
	}
	else
	{
		return "?page=products&amp;cat=" . $cat->cat_ID;
	}
}

$smarty->register_function("buildleftmenu", "build_left_menu_item"); 
?>
